//
//  CZMainViewController.swift
//  iOSWeibo10
//
//  Created by 李伟鹏 on 16/5/10.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZMainViewController: UITabBarController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let composeCallBack = { () -> () in
            if !CZUserAccountViewModel.sharedInstance.isUserLogin{
                let oauthVc = CZOauthViewController()
                let nav = UINavigationController(rootViewController: oauthVc)
                self.presentViewController(nav, animated: true, completion: nil)
            }else{
                let composeVc = CZComposeViewController()
                let nav = UINavigationController(rootViewController: composeVc)
                self.presentViewController(nav, animated: true, completion: nil)
            }
        }
        let mainTabBar = CZMainTabBar()
        mainTabBar.composeCallBack = composeCallBack
        setValue(mainTabBar, forKey: "tabBar")
        setupChilderViewController()
        
    }

    private func setupChilderViewController(){
        let homeVc = CZHomeViewController()
        self.addChildViewControllers(homeVc, title: "首页", imageName: "tabbar_home")
        
        let messageVc = CZMessageViewController()
        self.addChildViewControllers(messageVc, title: "消息", imageName: "tabbar_message_center")
        
        let discoverVc = CZDiscoveryViewController()
        self.addChildViewControllers(discoverVc, title: "发现", imageName: "tabbar_discover")
        
        let profileVc = CZProfileViewController()
        self.addChildViewControllers(profileVc, title: "我", imageName: "tabbar_profile")
    }
    
    private func addChildViewControllers(controller:UIViewController, title:String, imageName:String){
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        controller.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Normal)
//        let profileNav = UINavigationController(rootViewController: controller)
        self.addChildViewController(UINavigationController(rootViewController: controller))
    }
}
